/**
 * @author temsa
 */

doh.registerGroup("tests.require",[
    function testAsynchronousRequireWithSimpleUrlStoringForHTML() {
        var d = new doh.Deferred();
        var storage={};
        storage[Archetype.path + "Archetype/unitTest/data/helloWorld.html"]=null;
		
        Archetype.require("Archetype/unitTest/data/helloWorld.html", {
            asynchronous: true,
            evaluation: "html",
            storing: storage
        });
        var checkCondition = function() {
            doh.assertNotEqual(storage[ Archetype.path + "Archetype/unitTest/data/helloWorld.html"], null, 'storage[ "' + Archetype.path + '"Archetype/unitTest/data/helloWorld.html"] should not be null ');
            doh.assertEqual("Hello, world!", storage[Archetype.path + "Archetype/unitTest/data/helloWorld.html"], "Stored result should be 'Hello, world!'")
        }
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    },
    function testAsynchronousRequireWithUmnAndStoringForHTML() {
        var d = new doh.Deferred();
        var storage={};
        storage[Archetype.path + "Archetype/unitTest/data/helloWorld.html"]=null;
		
        doh.assertEqual(Archetype.path + "Archetype/unitTest/data/helloWorld.html",$U("Archetype.unitTest.data.helloWorld","html"));
		
        Archetype.require("Archetype.unitTest.data.helloWorld", {
            asynchronous: true,
            evaluation: "html",
            storing: storage
        });
        
        var checkCondition = function() {
            Logger.debug("testAsynchronousRequireWithUmnAndStoring storage", storage);
            doh.assertEqual("Hello, world!", storage[Archetype.path + "Archetype/unitTest/data/helloWorld.html"], "Stored result should be there after 500ms (but may not be there due to server time response)")
        }
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    },

    function testSynchronousRequireWithUmnAndStoringForHTML() {
        Logger.debug("start of synchronous");

        var storage={};
        var key=$U("Archetype.unitTest.data.helloWorld","html");
        Logger.debug("key:",key);
        storage[key]=null;
		
        Archetype.require(key, {
            asynchronous: false,
            evaluation: "html",
            storing: storage
        });
		
        Logger.log("testSynchronousRequireWithUmnAndStoring storage", storage);
        doh.assertEqual("Hello, world!", storage[key], "Stored result should be there");
        
    },
    function testAsynchronousRequireWithUmnForCSS() {
        var d = new doh.Deferred();
        Archetype.require("Archetype.unitTest.styles.dummytest", {
            asynchronous: true,
            evaluation: "css"
        });
		
        var checkCondition =  function() {
            Logger.log("testAsynchronousRequireWithUmnForCSS");
            doh.assertEqual(Archetype.loaderQueue.queue.length, 0, "The Archetype.loaderQueue.queue should be empty after 500ms (but may not be due to server time response)");
            doh.assertEqual(Archetype.loaderQueue.currents.length, 0, "The Archetype.loaderQueue.currents should be empty after 500ms (but may not be due to server time response)");
            doh.assertEqual(Archetype.loaderQueue.callbacks.length, 0, "The Archetype.loaderQueue.callbacks should be empty after 500ms (but may not be due to server time response)");
        }
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    },
    function testNeedsSimpleDependency() {
        var d = new doh.Deferred();
        Archetype.modules["Archetype.unitTest.data.NeedsSimpleDependencie"] = {
            needs: "Archetype.unitTest.data.NeededNeedsSimpleDependencie"
        }
        Archetype.require("Archetype.unitTest.data.NeedsSimpleDependencie");
		
        var checkCondition =  function(){
            doh.assertEqual(Archetype.loaderQueue.queue.length, 0, "The Archetype.loaderQueue.queue should be empty after 500ms (but may not be due to server time response)");
            doh.assertEqual(Archetype.loaderQueue.currents.length, 0, "The Archetype.loaderQueue.currents should be empty after 500ms (but may not be due to server time response)");
            doh.assertEqual(Archetype.loaderQueue.callbacks.length, 0, "The Archetype.loaderQueue.callbacks should be empty after 500ms (but may not be due to server time response)");
			
            doh.assertEqual(true, window.NeedsSimpleDependencie, "Archetype.unitTest.data.NeedsSimpleDependencie has not been loaded");
            doh.assertEqual(true, window.NeededNeedsSimpleDependencie, "Archetype.unitTest.data.NeededNeedsSimpleDependencie has not been loaded");
        }
        setTimeout(d.getTestCallback(checkCondition), 200);
        return d;
    }
]);


